/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.hotel.test.service.persistence;

import com.hotel.test.NoSuchLocalException;
import com.hotel.test.model.Local;
import com.hotel.test.model.impl.LocalImpl;
import com.hotel.test.model.impl.LocalModelImpl;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.RowMapper;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the local service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Vien-IT
 * @see LocalPersistence
 * @see LocalUtil
 * @generated
 */
public class LocalPersistenceImpl extends BasePersistenceImpl<Local>
	implements LocalPersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link LocalUtil} to access the local persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = LocalImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_LOCALNAME =
		new FinderPath(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalModelImpl.FINDER_CACHE_ENABLED, LocalImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByLocalName",
			new String[] {
				String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LOCALNAME =
		new FinderPath(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalModelImpl.FINDER_CACHE_ENABLED, LocalImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByLocalName",
			new String[] { String.class.getName() },
			LocalModelImpl.LOCALNAME_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_LOCALNAME = new FinderPath(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByLocalName",
			new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalModelImpl.FINDER_CACHE_ENABLED, LocalImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalModelImpl.FINDER_CACHE_ENABLED, LocalImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);

	/**
	 * Caches the local in the entity cache if it is enabled.
	 *
	 * @param local the local
	 */
	public void cacheResult(Local local) {
		EntityCacheUtil.putResult(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalImpl.class, local.getPrimaryKey(), local);

		local.resetOriginalValues();
	}

	/**
	 * Caches the locals in the entity cache if it is enabled.
	 *
	 * @param locals the locals
	 */
	public void cacheResult(List<Local> locals) {
		for (Local local : locals) {
			if (EntityCacheUtil.getResult(LocalModelImpl.ENTITY_CACHE_ENABLED,
						LocalImpl.class, local.getPrimaryKey()) == null) {
				cacheResult(local);
			}
			else {
				local.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all locals.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(LocalImpl.class.getName());
		}

		EntityCacheUtil.clearCache(LocalImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the local.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(Local local) {
		EntityCacheUtil.removeResult(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalImpl.class, local.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@Override
	public void clearCache(List<Local> locals) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (Local local : locals) {
			EntityCacheUtil.removeResult(LocalModelImpl.ENTITY_CACHE_ENABLED,
				LocalImpl.class, local.getPrimaryKey());
		}
	}

	/**
	 * Creates a new local with the primary key. Does not add the local to the database.
	 *
	 * @param localId the primary key for the new local
	 * @return the new local
	 */
	public Local create(long localId) {
		Local local = new LocalImpl();

		local.setNew(true);
		local.setPrimaryKey(localId);

		return local;
	}

	/**
	 * Removes the local with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param localId the primary key of the local
	 * @return the local that was removed
	 * @throws com.hotel.test.NoSuchLocalException if a local with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Local remove(long localId)
		throws NoSuchLocalException, SystemException {
		return remove(Long.valueOf(localId));
	}

	/**
	 * Removes the local with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the local
	 * @return the local that was removed
	 * @throws com.hotel.test.NoSuchLocalException if a local with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public Local remove(Serializable primaryKey)
		throws NoSuchLocalException, SystemException {
		Session session = null;

		try {
			session = openSession();

			Local local = (Local)session.get(LocalImpl.class, primaryKey);

			if (local == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchLocalException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(local);
		}
		catch (NoSuchLocalException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected Local removeImpl(Local local) throws SystemException {
		local = toUnwrappedModel(local);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, local);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		clearCache(local);

		return local;
	}

	@Override
	public Local updateImpl(com.hotel.test.model.Local local, boolean merge)
		throws SystemException {
		local = toUnwrappedModel(local);

		boolean isNew = local.isNew();

		LocalModelImpl localModelImpl = (LocalModelImpl)local;

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, local, merge);

			local.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew || !LocalModelImpl.COLUMN_BITMASK_ENABLED) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		else {
			if ((localModelImpl.getColumnBitmask() &
					FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LOCALNAME.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						localModelImpl.getOriginalLocalName()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_LOCALNAME,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LOCALNAME,
					args);

				args = new Object[] { localModelImpl.getLocalName() };

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_LOCALNAME,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LOCALNAME,
					args);
			}
		}

		EntityCacheUtil.putResult(LocalModelImpl.ENTITY_CACHE_ENABLED,
			LocalImpl.class, local.getPrimaryKey(), local);

		return local;
	}

	protected Local toUnwrappedModel(Local local) {
		if (local instanceof LocalImpl) {
			return local;
		}

		LocalImpl localImpl = new LocalImpl();

		localImpl.setNew(local.isNew());
		localImpl.setPrimaryKey(local.getPrimaryKey());

		localImpl.setLocalId(local.getLocalId());
		localImpl.setUserId(local.getUserId());
		localImpl.setUserName(local.getUserName());
		localImpl.setCreateDate(local.getCreateDate());
		localImpl.setModifiedDate(local.getModifiedDate());
		localImpl.setLocalName(local.getLocalName());
		localImpl.setIntroduce(local.getIntroduce());
		localImpl.setImage(local.getImage());

		return localImpl;
	}

	/**
	 * Returns the local with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the local
	 * @return the local
	 * @throws com.liferay.portal.NoSuchModelException if a local with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public Local findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the local with the primary key or throws a {@link com.hotel.test.NoSuchLocalException} if it could not be found.
	 *
	 * @param localId the primary key of the local
	 * @return the local
	 * @throws com.hotel.test.NoSuchLocalException if a local with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Local findByPrimaryKey(long localId)
		throws NoSuchLocalException, SystemException {
		Local local = fetchByPrimaryKey(localId);

		if (local == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + localId);
			}

			throw new NoSuchLocalException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				localId);
		}

		return local;
	}

	/**
	 * Returns the local with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the local
	 * @return the local, or <code>null</code> if a local with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public Local fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the local with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param localId the primary key of the local
	 * @return the local, or <code>null</code> if a local with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Local fetchByPrimaryKey(long localId) throws SystemException {
		Local local = (Local)EntityCacheUtil.getResult(LocalModelImpl.ENTITY_CACHE_ENABLED,
				LocalImpl.class, localId);

		if (local == _nullLocal) {
			return null;
		}

		if (local == null) {
			Session session = null;

			boolean hasException = false;

			try {
				session = openSession();

				local = (Local)session.get(LocalImpl.class,
						Long.valueOf(localId));
			}
			catch (Exception e) {
				hasException = true;

				throw processException(e);
			}
			finally {
				if (local != null) {
					cacheResult(local);
				}
				else if (!hasException) {
					EntityCacheUtil.putResult(LocalModelImpl.ENTITY_CACHE_ENABLED,
						LocalImpl.class, localId, _nullLocal);
				}

				closeSession(session);
			}
		}

		return local;
	}

	/**
	 * Returns all the locals where localName = &#63;.
	 *
	 * @param localName the local name
	 * @return the matching locals
	 * @throws SystemException if a system exception occurred
	 */
	public List<Local> findByLocalName(String localName)
		throws SystemException {
		return findByLocalName(localName, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
			null);
	}

	/**
	 * Returns a range of all the locals where localName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param localName the local name
	 * @param start the lower bound of the range of locals
	 * @param end the upper bound of the range of locals (not inclusive)
	 * @return the range of matching locals
	 * @throws SystemException if a system exception occurred
	 */
	public List<Local> findByLocalName(String localName, int start, int end)
		throws SystemException {
		return findByLocalName(localName, start, end, null);
	}

	/**
	 * Returns an ordered range of all the locals where localName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param localName the local name
	 * @param start the lower bound of the range of locals
	 * @param end the upper bound of the range of locals (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of matching locals
	 * @throws SystemException if a system exception occurred
	 */
	public List<Local> findByLocalName(String localName, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_LOCALNAME;
			finderArgs = new Object[] { localName };
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_LOCALNAME;
			finderArgs = new Object[] { localName, start, end, orderByComparator };
		}

		List<Local> list = (List<Local>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_LOCAL_WHERE);

			if (localName == null) {
				query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_1);
			}
			else {
				if (localName.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_3);
				}
				else {
					query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_2);
				}
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(LocalModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (localName != null) {
					qPos.add(localName);
				}

				list = (List<Local>)QueryUtil.list(q, getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Returns the first local in the ordered set where localName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param localName the local name
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching local
	 * @throws com.hotel.test.NoSuchLocalException if a matching local could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Local findByLocalName_First(String localName,
		OrderByComparator orderByComparator)
		throws NoSuchLocalException, SystemException {
		List<Local> list = findByLocalName(localName, 0, 1, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("localName=");
			msg.append(localName);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchLocalException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Returns the last local in the ordered set where localName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param localName the local name
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching local
	 * @throws com.hotel.test.NoSuchLocalException if a matching local could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Local findByLocalName_Last(String localName,
		OrderByComparator orderByComparator)
		throws NoSuchLocalException, SystemException {
		int count = countByLocalName(localName);

		List<Local> list = findByLocalName(localName, count - 1, count,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("localName=");
			msg.append(localName);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchLocalException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Returns the locals before and after the current local in the ordered set where localName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param localId the primary key of the current local
	 * @param localName the local name
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the previous, current, and next local
	 * @throws com.hotel.test.NoSuchLocalException if a local with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Local[] findByLocalName_PrevAndNext(long localId, String localName,
		OrderByComparator orderByComparator)
		throws NoSuchLocalException, SystemException {
		Local local = findByPrimaryKey(localId);

		Session session = null;

		try {
			session = openSession();

			Local[] array = new LocalImpl[3];

			array[0] = getByLocalName_PrevAndNext(session, local, localName,
					orderByComparator, true);

			array[1] = local;

			array[2] = getByLocalName_PrevAndNext(session, local, localName,
					orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Local getByLocalName_PrevAndNext(Session session, Local local,
		String localName, OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_LOCAL_WHERE);

		if (localName == null) {
			query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_1);
		}
		else {
			if (localName.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_3);
			}
			else {
				query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

			if (orderByConditionFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByConditionFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByConditionFields[i]);

				if ((i + 1) < orderByConditionFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			String[] orderByFields = orderByComparator.getOrderByFields();

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(LocalModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		if (localName != null) {
			qPos.add(localName);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByConditionValues(local);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<Local> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Returns all the locals.
	 *
	 * @return the locals
	 * @throws SystemException if a system exception occurred
	 */
	public List<Local> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the locals.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of locals
	 * @param end the upper bound of the range of locals (not inclusive)
	 * @return the range of locals
	 * @throws SystemException if a system exception occurred
	 */
	public List<Local> findAll(int start, int end) throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the locals.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of locals
	 * @param end the upper bound of the range of locals (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of locals
	 * @throws SystemException if a system exception occurred
	 */
	public List<Local> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = new Object[] { start, end, orderByComparator };

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<Local> list = (List<Local>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_LOCAL);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_LOCAL.concat(LocalModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<Local>)QueryUtil.list(q, getDialect(), start,
							end, false);

					Collections.sort(list);
				}
				else {
					list = (List<Local>)QueryUtil.list(q, getDialect(), start,
							end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the locals where localName = &#63; from the database.
	 *
	 * @param localName the local name
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByLocalName(String localName) throws SystemException {
		for (Local local : findByLocalName(localName)) {
			remove(local);
		}
	}

	/**
	 * Removes all the locals from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (Local local : findAll()) {
			remove(local);
		}
	}

	/**
	 * Returns the number of locals where localName = &#63;.
	 *
	 * @param localName the local name
	 * @return the number of matching locals
	 * @throws SystemException if a system exception occurred
	 */
	public int countByLocalName(String localName) throws SystemException {
		Object[] finderArgs = new Object[] { localName };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_LOCALNAME,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_LOCAL_WHERE);

			if (localName == null) {
				query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_1);
			}
			else {
				if (localName.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_3);
				}
				else {
					query.append(_FINDER_COLUMN_LOCALNAME_LOCALNAME_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (localName != null) {
					qPos.add(localName);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_LOCALNAME,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of locals.
	 *
	 * @return the number of locals
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_LOCAL);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns all the hotels associated with the local.
	 *
	 * @param pk the primary key of the local
	 * @return the hotels associated with the local
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.hotel.test.model.Hotel> getHotels(long pk)
		throws SystemException {
		return getHotels(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
	}

	/**
	 * Returns a range of all the hotels associated with the local.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the local
	 * @param start the lower bound of the range of locals
	 * @param end the upper bound of the range of locals (not inclusive)
	 * @return the range of hotels associated with the local
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.hotel.test.model.Hotel> getHotels(long pk, int start,
		int end) throws SystemException {
		return getHotels(pk, start, end, null);
	}

	public static final FinderPath FINDER_PATH_GET_HOTELS = new FinderPath(com.hotel.test.model.impl.HotelModelImpl.ENTITY_CACHE_ENABLED,
			com.hotel.test.model.impl.HotelModelImpl.FINDER_CACHE_ENABLED,
			com.hotel.test.model.impl.HotelImpl.class,
			com.hotel.test.service.persistence.HotelPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getHotels",
			new String[] {
				Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});

	/**
	 * Returns an ordered range of all the hotels associated with the local.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the local
	 * @param start the lower bound of the range of locals
	 * @param end the upper bound of the range of locals (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of hotels associated with the local
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.hotel.test.model.Hotel> getHotels(long pk, int start,
		int end, OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };

		List<com.hotel.test.model.Hotel> list = (List<com.hotel.test.model.Hotel>)FinderCacheUtil.getResult(FINDER_PATH_GET_HOTELS,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				String sql = null;

				if (orderByComparator != null) {
					sql = _SQL_GETHOTELS.concat(ORDER_BY_CLAUSE)
										.concat(orderByComparator.getOrderBy());
				}
				else {
					sql = _SQL_GETHOTELS.concat(com.hotel.test.model.impl.HotelModelImpl.ORDER_BY_SQL);
				}

				SQLQuery q = session.createSQLQuery(sql);

				q.addEntity("HT_Hotel",
					com.hotel.test.model.impl.HotelImpl.class);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				list = (List<com.hotel.test.model.Hotel>)QueryUtil.list(q,
						getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_GET_HOTELS,
						finderArgs);
				}
				else {
					hotelPersistence.cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_GET_HOTELS,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	public static final FinderPath FINDER_PATH_GET_HOTELS_SIZE = new FinderPath(com.hotel.test.model.impl.HotelModelImpl.ENTITY_CACHE_ENABLED,
			com.hotel.test.model.impl.HotelModelImpl.FINDER_CACHE_ENABLED,
			com.hotel.test.model.impl.HotelImpl.class,
			com.hotel.test.service.persistence.HotelPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getHotelsSize", new String[] { Long.class.getName() });

	/**
	 * Returns the number of hotels associated with the local.
	 *
	 * @param pk the primary key of the local
	 * @return the number of hotels associated with the local
	 * @throws SystemException if a system exception occurred
	 */
	public int getHotelsSize(long pk) throws SystemException {
		Object[] finderArgs = new Object[] { pk };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_HOTELS_SIZE,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				SQLQuery q = session.createSQLQuery(_SQL_GETHOTELSSIZE);

				q.addScalar(COUNT_COLUMN_NAME,
					com.liferay.portal.kernel.dao.orm.Type.LONG);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_GET_HOTELS_SIZE,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	public static final FinderPath FINDER_PATH_CONTAINS_HOTEL = new FinderPath(com.hotel.test.model.impl.HotelModelImpl.ENTITY_CACHE_ENABLED,
			com.hotel.test.model.impl.HotelModelImpl.FINDER_CACHE_ENABLED,
			com.hotel.test.model.impl.HotelImpl.class,
			com.hotel.test.service.persistence.HotelPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"containsHotel",
			new String[] { Long.class.getName(), Long.class.getName() });

	/**
	 * Returns <code>true</code> if the hotel is associated with the local.
	 *
	 * @param pk the primary key of the local
	 * @param hotelPK the primary key of the hotel
	 * @return <code>true</code> if the hotel is associated with the local; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsHotel(long pk, long hotelPK)
		throws SystemException {
		Object[] finderArgs = new Object[] { pk, hotelPK };

		Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_HOTEL,
				finderArgs, this);

		if (value == null) {
			try {
				value = Boolean.valueOf(containsHotel.contains(pk, hotelPK));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (value == null) {
					value = Boolean.FALSE;
				}

				FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_HOTEL,
					finderArgs, value);
			}
		}

		return value.booleanValue();
	}

	/**
	 * Returns <code>true</code> if the local has any hotels associated with it.
	 *
	 * @param pk the primary key of the local to check for associations with hotels
	 * @return <code>true</code> if the local has any hotels associated with it; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsHotels(long pk) throws SystemException {
		if (getHotelsSize(pk) > 0) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * Initializes the local persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.hotel.test.model.Local")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<Local>> listenersList = new ArrayList<ModelListener<Local>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<Local>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}

		containsHotel = new ContainsHotel();
	}

	public void destroy() {
		EntityCacheUtil.removeCache(LocalImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@BeanReference(type = BookingPersistence.class)
	protected BookingPersistence bookingPersistence;
	@BeanReference(type = HotelPersistence.class)
	protected HotelPersistence hotelPersistence;
	@BeanReference(type = LocalPersistence.class)
	protected LocalPersistence localPersistence;
	@BeanReference(type = RomTypePersistence.class)
	protected RomTypePersistence romTypePersistence;
	@BeanReference(type = TblCommentPersistence.class)
	protected TblCommentPersistence tblCommentPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	protected ContainsHotel containsHotel;

	protected class ContainsHotel {
		protected ContainsHotel() {
			_mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
					_SQL_CONTAINSHOTEL,
					new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
					RowMapper.COUNT);
		}

		protected boolean contains(long localId, long hotelId) {
			List<Integer> results = _mappingSqlQuery.execute(new Object[] {
						new Long(localId), new Long(hotelId)
					});

			if (results.size() > 0) {
				Integer count = results.get(0);

				if (count.intValue() > 0) {
					return true;
				}
			}

			return false;
		}

		private MappingSqlQuery<Integer> _mappingSqlQuery;
	}

	private static final String _SQL_SELECT_LOCAL = "SELECT local FROM Local local";
	private static final String _SQL_SELECT_LOCAL_WHERE = "SELECT local FROM Local local WHERE ";
	private static final String _SQL_COUNT_LOCAL = "SELECT COUNT(local) FROM Local local";
	private static final String _SQL_COUNT_LOCAL_WHERE = "SELECT COUNT(local) FROM Local local WHERE ";
	private static final String _SQL_GETHOTELS = "SELECT {HT_Hotel.*} FROM HT_Hotel INNER JOIN HT_Local ON (HT_Local.localId = HT_Hotel.localId) WHERE (HT_Local.localId = ?)";
	private static final String _SQL_GETHOTELSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM HT_Hotel WHERE localId = ?";
	private static final String _SQL_CONTAINSHOTEL = "SELECT COUNT(*) AS COUNT_VALUE FROM HT_Hotel WHERE localId = ? AND hotelId = ?";
	private static final String _FINDER_COLUMN_LOCALNAME_LOCALNAME_1 = "local.localName IS NULL";
	private static final String _FINDER_COLUMN_LOCALNAME_LOCALNAME_2 = "local.localName = ?";
	private static final String _FINDER_COLUMN_LOCALNAME_LOCALNAME_3 = "(local.localName IS NULL OR local.localName = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "local.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Local exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Local exists with the key {";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(LocalPersistenceImpl.class);
	private static Local _nullLocal = new LocalImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<Local> toCacheModel() {
				return _nullLocalCacheModel;
			}
		};

	private static CacheModel<Local> _nullLocalCacheModel = new CacheModel<Local>() {
			public Local toEntityModel() {
				return _nullLocal;
			}
		};
}